// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

import static com.geniusai.aip.auth.constants.LoadConstants.BCRYPT_CONFIG_LONG_ROUND;

/**
 * @author longping58607
 * @date 2020/12/9
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(BCRYPT_CONFIG_LONG_ROUND);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                // 授权配置
                // 无需认证
                .antMatchers("/v3/api-docs", "/swagger-resources/**", "/swagger-ui/**", "/v1/*", "/v1/user/**", "/oauth/jwks.json").permitAll()
                .anyRequest()
                // 所有请求
                // 都需要认证
                .authenticated()
                .and().csrf().disable();
    }

}
